﻿// -----------------------------------------------------------------------
// <copyright file="RouteConfigBuilder.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace NetFramework.Router
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Routing;
    using NetFramework.Router.Configuration;
    using NetFramework.Configuration;
using NetFramework.Logging;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class RouteConfigBuilder
    {
        private static ILogger log = LoggerManager.GetLog("NetFramework.Routing", "RouteConfigBuilder ");
        private static RouteCollection routeCollection;
        private static bool addSystemRouteTable = true;

        public static void Build()
        {
            // 获取页面相对路径配置信息
            PageInfoConfig pageInfoConfig = ConfigManager.GetConfig<PageInfoConfig>();
            if (pageInfoConfig == null)
            {
                log.Info("PageInfoConfig IS NULL");
                return;
            }

            // 初始化路由信息
            RouteConfig routeConfig = ConfigManager.GetConfig<RouteConfig>();
            if (routeConfig == null)
            {
                log.Info("RouteConfig IS NULL");
                return;
            }

            if (!routeConfig.Enabled || 
                routeConfig.PagedRouteInfo == null ||
                routeConfig.PagedRouteInfo.Count == 0)
            {
                log.Info("routeConfig.Enabled is false OR routeConfig.PagedRouteInfo EMPTY");
                return;
            }

            if (!addSystemRouteTable)
            {
                routeCollection = new RouteCollection();
            }

            List<PagedRouteInfo> pagedRouteInfoList = routeConfig.PagedRouteInfo;
            foreach (PagedRouteInfo pagedRouteInfo in pagedRouteInfoList)
            {
                if (pagedRouteInfo.Routes == null)
                {
                    continue;
                }
                PageInfo page = pageInfoConfig.GetPageInfo(pagedRouteInfo.PageName);

                foreach (RouteInfo routeInfo in pagedRouteInfo.Routes)
                {
                    string virtualPath = page == null ? routeInfo.RelatedPath : page.RelatedPath;
                    string name = page == null ? routeInfo.PageName : page.PageName;

                    Route route = new Route(routeInfo.Url, new NetFrameworkPageRouteHandler("~/" + virtualPath));
                    if (!string.IsNullOrEmpty(routeInfo.Defaults))
                    {
                        route.Defaults = ConvertToeDictionary(routeInfo.Defaults);
                    }


                    if (addSystemRouteTable)
                    {
                        RouteTable.Routes.Add(route);
                    }
                    else
                    {
                        routeCollection.Add(route);
                    }
                }
            }

            if (addSystemRouteTable)
            {
                RouteTable.Routes.RouteExistingFiles = true;
            }
            else
            {
                routeCollection.RouteExistingFiles = true;
            }
        }

        public static RouteCollection Routes
        {
            get
            {
                if (addSystemRouteTable)
                {
                    return RouteTable.Routes;
                }
                else
                {
                    return routeCollection;
                }
            }
        }

        private static RouteValueDictionary ConvertToeDictionary(string value)
        {
            RouteValueDictionary defaults = new RouteValueDictionary();
            string[] strArray = value.Trim().Split(new char[] { ',' });
            if (strArray.Length <= 0)
            {
                throw new ArgumentException("RouteBuilder: Defaults value is empty or malformed.");
            }
            foreach (string str in strArray)
            {
                string[] strArray2 = str.Trim().Split(new char[] { '=' });
                if ((strArray2.Length % 2) != 0)
                {
                    throw new ArgumentException("RouteBuilder: All Keys in Defaults must have values!");
                }

                defaults.Add(strArray2[0], strArray2[1]);
            }

            return defaults;
        }
    }
}
